package org.ykhl.log.commons.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by zkf on 2015/12/10.
 */
public class LogMessage {
    public enum LogType {RECEIVE_FROM_REQUEST,RECEIVE_FROM_RESTFUL, RECEIVE_FROM_MQHOS,RECEIVE_FROM_HOSPITAL,RECEIVE_FROM_TRANSFORM,
        SEND_TO_RESTFUL,SEND_TO_MQHOS, SEND_TO_HOSPITAL, SEND_TO_TRANSFORM,SEND_TO_RESPONSE,STARTUP, SHUTDOWN, ERROR}

    public static final String TransformId = "Transform";
    public static final String HospitalId = "Hospital";

    private LogType logType;
    private String serverIp;
    private long timestamp;
    private long bornTimestamp;

    public final LogType getLogType() {
        return logType;
    }

    public final void setLogType(LogType logType) {
        this.logType = logType;
    }

    public final String getServerIp() {
        return serverIp;
    }

    public final void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public final long getTimestamp() {
        return timestamp;
    }

    public final void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public long getBornTimestamp() {
        return bornTimestamp;
    }

    public void setBornTimestamp(long bornTimestamp) {
        this.bornTimestamp = bornTimestamp;
    }

    public final String toJson(){
        return JSON.toJSONString(this);
    }

    public static LogMessage fromJson(String json){
        JSONObject jsonObject = JSON.parseObject(json);
        LogType logType = LogType.valueOf(jsonObject.getString("logType"));
       
        switch (logType){
            case STARTUP:
            case SHUTDOWN:
                return JSON.toJavaObject(jsonObject, LogMessage.class);
            case RECEIVE_FROM_REQUEST:
            case SEND_TO_MQHOS:
            case RECEIVE_FROM_RESTFUL:
            case RECEIVE_FROM_HOSPITAL:
            case SEND_TO_TRANSFORM:
            case RECEIVE_FROM_TRANSFORM:
            case SEND_TO_RESTFUL:
            case RECEIVE_FROM_MQHOS:
            case SEND_TO_RESPONSE:
            case SEND_TO_HOSPITAL:
                return JSON.toJavaObject(jsonObject, ReceiveSendLogMessage.class);
            case ERROR:
                return JSON.toJavaObject(jsonObject, ErrorLogMessage.class);
        }

        return null;
    }
}
